﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace common
{
   public static class Tools
    {

        public static void OpenWindow(Form view,Control mainPanel)
        {
            //开新窗口前，关闭其他窗口
            foreach (Control j in mainPanel.Controls)
            {
                if (j is Form)
                {
                    Form k = j as Form;
                    k.Close();

                }
            }

            int num = 0;
            //不可为顶层窗体,如果是顶层窗体则不能设置parent属性
            view.TopLevel = false;
            //父元素设置为mian
            view.Parent = mainPanel;


            foreach (Control i in mainPanel.Controls)
            {
                //如何这个子控件是窗体
                if (i is Form)
                {
                    if (i.Name == view.Name)
                    {
                        num++;

                    }
                }
            }

            if (num == 1)
            {
                //view.StartPosition = FormStartPosition.CenterScreen;
                view.Width = mainPanel.Width;
                view.Height = mainPanel.Height;

                view.Show();
            }
        }
        public static void XMLAttributeAppend(
            XmlDocument rootxml,string name,string value,
            XmlElement head
            )
        {
            XmlAttribute att = rootxml.CreateAttribute(name);
            att.Value = value;
            head.SetAttribute(name,value);
        }
        public static string GetXMLValue(XmlNode rootxml,string name)
        {
            if(rootxml != null && rootxml.Attributes != null &&rootxml.Attributes[name] != null)
            {
                return rootxml.Attributes[name].Value;
            }
            return "";
        }

    }
}
